//
//  MovieCollectViewController.m
//  豆瓣终极版
//
//  Created by dllo on 15/12/19.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "MovieCollectViewController.h"
#import "DataBaseSingleton.h"
#import "MovieDetailViewController.h"
@interface MovieCollectViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *collectTableView;

@end

@implementation MovieCollectViewController

- (void)dealloc{
    [_collectTableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影收藏";
    self.collectTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.collectTableView];
    self.collectTableView.delegate = self;
    self.collectTableView.dataSource = self;
    [_collectTableView release];
    
    [self.collectTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[DataBaseSingleton shareDataBase] selectMovieTable].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSMutableArray *array = [[DataBaseSingleton shareDataBase] selectMovieTable];
    Movie *movie = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = movie.movieName;
//    NSLog(@"movieName = %@", movie.movieName);
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetailViewController *movieDetail = [[MovieDetailViewController alloc] init];
    NSMutableArray *array = [[DataBaseSingleton shareDataBase] selectMovieTable];
    Movie *movie = [array objectAtIndex:indexPath.row];
    movieDetail.movieId = movie.movieId;
    [self.navigationController pushViewController:movieDetail animated:YES];
    
}

// 编辑删除
// 点击系统编辑按钮触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [self.collectTableView setEditing:editing animated:animated];
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *array = [[DataBaseSingleton shareDataBase] selectMovieTable];
    Movie *movie = [array objectAtIndex:indexPath.row];
    [[DataBaseSingleton shareDataBase] deleteMovieTable:movie.movieName];
    [self.collectTableView reloadData];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
